//
//  HXFoodViewFlowLayout.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/1.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXFoodViewFlowLayout.h"
#import "UIView+Frame.h"
#import "HX_Macro.h"
@interface HXFoodViewFlowLayout ()

@property (nonatomic, assign) CGFloat itemWidth;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, assign) CGFloat itemLine;
@property (nonatomic, assign) CGFloat numberOfLines;
@property (nonatomic, assign) CGFloat itemW;
@end
@implementation HXFoodViewFlowLayout
- (instancetype)init {
    self = [super init];
    if (self) {
        _attribuesArrays = [NSMutableArray array];
        _numberOfColumns = 4;
        _numberOfLines = 5;
        _contentHeight = 0.0f;
        _cellMargin = 5.0f;
        _itemLine = 2;
    }
    return self;
}
- (CGFloat)itemW {
    CGFloat allMar = (_numberOfLines + 2) * _cellMargin;
    CGFloat noMarg = CGRectGetWidth(self.collectionView.bounds) - allMar;
    return noMarg / _numberOfLines;
}
- (CGFloat)itemWidth {
    CGFloat allMargin = (CGRectGetWidth(self.collectionView.bounds) - (_numberOfLines + 1) * _cellMargin) / _numberOfLines;
    return allMargin;
}
- (void)prepareLayout {
    //    (CGRectGetWidth(self.collectionView.bounds) - (_numberOfLines + 1) * _cellMargin) / _numberOfLines;
    if (_attribuesArrays.count != 0) {
        [_attribuesArrays removeAllObjects];
    }
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        UICollectionViewLayoutAttributes *layout = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        CGFloat x = _cellMargin;
        CGFloat y = 0;
        CGFloat width = self.itemWidth;
        CGFloat height = 40;
        NSInteger j = 0;
        NSInteger cellRow = 0;
        if (i == 0) {
            width = 2 * self.itemWidth + _cellMargin;
            height = 2 * 40 + _cellMargin;
        } else if (i < 7) {
            x = (width + _cellMargin) * i + _cellMargin * 2 + self.itemWidth;
            y = 0;
            if (i >= 4) {
                x = _attribuesArrays[i - 3].frame.origin.x;
                y = height + _cellMargin;
            }
        } else {
            j = (i - 7);
            x = _cellMargin + j * (self.itemWidth + _cellMargin);
            if (cellRow != j / 5) {
                x = _attribuesArrays[i - 5].frame.origin.x;
            }
            cellRow = j / 5;
            y = _attribuesArrays[0].frame.size.height + _cellMargin + cellRow * (height + _cellMargin) + cellRow * (_cellMargin);
        }
        layout.frame = CGRectMake(x, y, width, height);
        [_attribuesArrays addObject:layout];
    }
    self.contentHeight = [self.attribuesArrays lastObject].frame.origin.y + [self.attribuesArrays lastObject].frame.size.height;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attribuesArrays;
}
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0, self.contentHeight);
}
@end
